package com.che.libcommon.ui.loading;

import android.support.annotation.IdRes;
import android.support.annotation.StyleRes;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;

import com.che.libcommon.api.ApiException;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.ui.VOMessage;
import com.che.libcommon.ui.loading.ILoadingView;
import com.che.libcommon.ui.loading.OnLoadingShowCallback;
import com.che.libcommon.widget.LoadingLayout;

/**
 * Created by zl on 2017/12/22.
 */

public class LoadingDelegate implements ILoadingView {

    private LoadingLayout loadingLayout;

    private ILoadingView.LoadingConfigure mLoadingConfigure;

    public LoadingDelegate(View rootView, @IdRes int containerLayoutId, ILoadingView.LoadingConfigure loadingConfigure) {
        this(rootView, rootView.findViewById(containerLayoutId), loadingConfigure);
    }

    /**
     *will remove the containerLayout,then add LoadingLayout to it,container will is LoadingLayout contentView
     * @param rootView
     * @param containerLayout
     * @param loadingConfigure
     */
    public LoadingDelegate(View rootView, View containerLayout, ILoadingView.LoadingConfigure loadingConfigure) {
        if (rootView == containerLayout) {
            throw new IllegalArgumentException("rootView containerLayout is ==,will throw stockflow exception");
        }

        mLoadingConfigure = loadingConfigure;
        View contentView = containerLayout;

        if (contentView != null) {

            loadingLayout = new LoadingLayout(rootView.getContext());

            if (rootView instanceof ViewGroup) {

                ViewGroup viewParent = (ViewGroup) rootView;

                LayoutParams layoutParams = contentView.getLayoutParams();

                int i = viewParent.indexOfChild(contentView);

                viewParent.removeView(contentView);
                loadingLayout.setContentView(contentView);
                if(loadingConfigure!=null){
                    setOnEmptyClickListener(loadingConfigure.getOnEmptyClickClickListener());
                    setOnRetryClickListener(loadingConfigure.getOnRetryClickListener());
                }
                viewParent.addView(loadingLayout, i, layoutParams);

            }

        }


    }

    @Override
    public void showLoading(boolean isShow) {
        if (loadingLayout != null) {
            loadingLayout.showLoading(isShow);
        }
        final OnLoadingShowCallback callback = getOnLoadingShowCallback();
        if (callback != null) {
            if (isShow) {
                callback.onShowProgress();
            } else {
                callback.onHideProgress();
            }

        }
    }

    @Override
    public void showError(VOMessage voError) {
        if (loadingLayout != null) {
            loadingLayout.showError(voError);
        }

        if (voError != null && voError.type == VOMessage.TYPE_EMPTY_DATA) {
            final OnLoadingShowCallback callback = getOnLoadingShowCallback();
            if (callback != null) {
                callback.onShowEmpty();
            }
        }
    }

    @Override
    public void showData() {
        if (loadingLayout != null) {
            loadingLayout.showData();
        }
        final OnLoadingShowCallback callback = getOnLoadingShowCallback();
        if (callback != null) {
            callback.onShowData();
        }
    }

    @Override
    public LoadingConfigure getConfigure() {
        return mLoadingConfigure;
    }

    public void showError(Throwable throwable) {
        showError(VOMessage.create(throwable));
        final OnLoadingShowCallback callback = getOnLoadingShowCallback();
        if (callback != null) {
            callback.onShowError(throwable);
        }
    }

    public void setOnRetryClickListener(OnClickListener callback) {
        if (loadingLayout != null) {
            loadingLayout.setOnRetryClickListener(callback);
        }
    }

    public void setOnEmptyClickListener(OnClickListener clickListener) {
        if (loadingLayout != null) {
            loadingLayout.setOnEmptyClickListener(clickListener);
        }
    }

    public void setTextMsgStyle(@StyleRes int tvMsgTitleResId, @StyleRes int tvMsgToResId) {
        if (loadingLayout != null) {
            loadingLayout.setTextMsgStyle(tvMsgTitleResId, tvMsgToResId);
        }
    }

    private OnLoadingShowCallback getOnLoadingShowCallback() {
        OnLoadingShowCallback callback = null;
        if (mLoadingConfigure != null) {
            callback = mLoadingConfigure.getOnLoadingShowCallback();
        }
        return callback;
    }

    public void showEmpty() {
        if (mLoadingConfigure != null) {
            showError(mLoadingConfigure.getEmptyMessage());
        }
    }

    public void showError(BaseResult result) {
        if (mLoadingConfigure != null) {
            showError(new ApiException("loading", result));
        }
    }

}
